library(tidyverse)
## -- Attaching packages ----------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts -------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(p8105.datasets)
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Loading data

data("instacart")

instacart1 = 
  instacart %>% 
  select(order_id, product_id, user_id, order_dow, order_hour_of_day, days_since_prior_order, product_name, aisle_id, aisle, department_id, department) %>% 
  sample_n(5000)

Plot 1

instacart1 %>% 
  group_by(department) %>% 
  count(aisle) %>% 
  mutate(department = factor(department),
         department = fct_reorder(department, n)) %>% 
  plot_ly(
    x = ~department, y = ~n, color = ~department, type = "bar",
    colors = "viridis"
  )
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

why cannot order department by number of aisles?

instacart1 %>% 
  count(aisle) %>% 
  mutate(aisle = factor(aisle),
         aisle = fct_reorder(aisle, n)) %>% 
  plot_ly(
    x = ~aisle, y = ~n, color = ~aisle, type = "bar",
    colors = "viridis"
  )

Plot 2

instacart1 %>% 
  mutate(department = factor(department),
         department = fct_reorder(department, order_hour_of_day)) %>% 
  plot_ly(
    x = ~department, y = ~order_hour_of_day, color = ~department, type = "box",
    colors = "viridis"
  )